/*
 ### jQuery Google Maps V3 Plugin v3.01 ###
 * Home: 
 * Code: 
 * Date: 2011-06-01
 * <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.1&sensor=false">
 * Dual licensed under the MIT and GPL licenses.
 *   http://www.gnu.org/licenses/gpl.html
 *   http://www.opensource.org/licenses/mit-license.php
 ###
*/
(function ($) {	
	var geocoder = new google.maps.Geocoder(); 
	$.googleMaps = {
		init:function(options){
			var opts = $.extend({}, $.googleMaps.defaults, options);
			return this.each(function() {
						// Create Map
						var center = opts.center,
							centerlatlng = $.googleMaps.getLatlng( opts.center);
						var mapOptions = {
								zoom: opts.zoom,
								center: centerlatlng,
								mapTypeControl: opts.mapTypeControl,
								zoomControl: opts.zoomControl,
								panControl : opts.panControl,
								scaleControl : opts.scaleControl,
								streetViewControl: opts.streetViewControl,
								scrollwheel: opts.scrollwheel,
								mapTypeId: opts.mapType
							};
						var $this = $(this),
							$gmap = new google.maps.Map(this, mapOptions);
							$this.data("$gmap", $gmap);
							$this.data('gmap', {
							   'opts': opts,
							   'gmap': $gmap,
							   'markers': [],
							   'infoWindow': null
							});
						if (opts.markers.length !== 0) {
							// Loop through marker array
							for (var i = 0; i < opts.markers.length; i+= 1) {
								$.googleMaps.addMarker.apply($this,[opts.markers[i]]);
							}
						}
						if(typeof opts.onComplete=='function'){
							opts.onComplete(centerlatlng);
						}
					});
		},
		geocode : function(address,callback){
			var $gmap = $(this).data("$gmap"),
				$data = this.data('gmap');
			geocoder.geocode( { 'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					var location = results[0].geometry.location;
					$gmap.setCenter(location);
					if($data.markers.length>0) $data.markers[0].setPosition(location);
					if(typeof callback=='function'){
						callback(location)
					}
				} else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		},
		addMarker: function (options) {
			var $gmap = $(this).data("$gmap"),
				$data = this.data('gmap');
			var position = (options.latlng&&options.latlng!=',')?$.googleMaps.getLatlng(options.latlng):$gmap.getCenter()
			var markerOptions = $.extend({}, {
				position : position,
				draggable:false,
				title:'',
				animation: google.maps.Animation.DROP,
				map:$gmap
				}, options);
			marker = new google.maps.Marker(markerOptions)
			$data.markers.push(marker);
			if(typeof options.callback=='function'){
				google.maps.event.addListener(marker, 'dragend', options.callback);
			}
			return marker;
		},
		getLatlng : function(s){
			return new google.maps.LatLng(s.split(',')[0], s.split(',')[1])
		},
		getMapCenter : function(){
			var $gmap = $(this).data("$gmap")
			return {lat:$gmap.getCenter().lat(),lng:$gmap.getCenter().lng()};
		},
		defaults: {
		// Default Map Options
			center:					'26.06875393605304,119.3123659650879',
			markers:                [],
			zoom: 					13,
			scroll: 				true,
			mapTypeControl:         true,
			zoomControl:            true,
			panControl :			true,
			streetViewControl:		false,
			scrollwheel : 			true,
			mapType : 				google.maps.MapTypeId.ROADMAP,
			controls: {
				hide: false,
				localSearch: false
			},
			icon: {
				image:               "http://www.google.com/mapfiles/marker.png",
				iconsize:            [20, 34],
				iconanchor:          [9, 34],
				infowindowanchor:    [9, 2],
				shadow:              "http://www.google.com/mapfiles/shadow50.png",
				shadowsize:          [37, 34]
			},
			onComplete:					null
		}
	}
    // Main plugin function
    $.fn.googleMaps = function (options) {
        // Method calling logic
        if ($.googleMaps[options]) {
            return $.googleMaps[options].apply(this,Array.prototype.slice.call(arguments,1));
        } else if (typeof options === 'object' || !options) {
            return $.googleMaps.init.apply(this,arguments);
        } else {
            $.error('Method ' +  options + ' does not exist on jQuery.gmap');
        }
    };
})(jQuery);
